#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
from .core._yamldatatodisk import YamlDataToDisk
import pymel.core as pm


class LayoutSetDataToDiskYaml(YamlDataToDisk):

    def scanScene(self):
        setDataDict = {}
        # ToDo
        setRefList = self._getSetRef()
        if not setRefList:
            return
        for setRef in setRefList:
            setRoot = self._getSetRoot(setRef)
            setData = self._getRootData(setRoot)
            setEnvList = setRoot.getChildren()
            envDataDict = {}
            for envRoot in setEnvList:
                envData = self._getRootData(envRoot)
                envDataDict[envRoot.longName()] = envData
            setData["env_assets"] = envDataDict
            setDataDict[setRoot.longName()]=setData
        return setDataDict
        
    def _getSetRoot(self, setRef):
        setRefNode = pm.PyNode(setRef)
        refNodes = setRefNode.nodes()
        setRoots = pm.ls(refNodes, type="transform")
        if len(setRoots) != 1:
            return
        return setRoots[0]
        
    def _getRootData(self, rootNode):
        nodeData = {}
        nodeLongName = rootNode.longName()
        #nodeData["name"] = nodeLongName
        nodeData["matrix"] = pm.xform(nodeLongName, q=1, m=1)
        nodeData["visibility"] = rootNode.isVisible()
        return nodeData
        
    def _getSetRef(self):
        resultRefList = []
        refList = pm.listReferences()
        for ref in refList:
            subRef = ref.subReferences()
            if subRef:
                resultRefList.append(ref.refNode)
        return resultRefList
                

